#include "Animal.h"

Animal::Animal(glm::vec3 _Pos, float _r, float _speed, std::string _Name,
		std::string _modelFName, ModelPool *_mPool, PathFinder *_pathfinder,
		Terrain::Terrain *ter) :
		dynamicEntity(_Pos, _r, _speed, _Name, _modelFName, _mPool, _pathfinder,
				ter)
{
	type = OBJ_ANIMAL;
	parentType = OBJ_DYNAMIC;
}

Animal::~Animal(void)
{
}

void Animal::updateAnimation()
{
}

void Animal::randomWandering()
{
	speed = basespeed;
	glm::vec2 randvec(rand() % (200 - 10) + 5, rand() % (200 - 10) + 5);
	if ((int) randvec.x != (int) Pos.x && (int) randvec.y != (int) Pos.y)
	{
		MoveVector = pathfinder->getPath(randvec, get2dPosition());
		resFlag = true;
		newMove = true;
	}
}

void Animal::detectIntruder()
{
	void *ret = pathfinder->getNearestEntityOfType(get2dPosition(), 5,
			OBJ_HUNTER);
	if (ret != NULL)
		speed = 0.1;
	else
		speed = basespeed;
}

void Animal::control(float t)
{
	if (timer.GetInterval(1000))
		detectIntruder();

	// exit when unit is sleeping
	if (sleeping)
		return;

	if (MoveVector.size() > 0)
	{

		move();
	}
	else
	{
		Moving = false;
		randomWandering();
	}

	updateAnimation();
	visible = World::world->SphereInFrustum(Pos, 1.0);
}
